package com.model;

import com.view.MyPanel;

/**
 * 方块类
 * 
 * @author Administrator
 *
 */
public class Shape {
	// 方块的类型
	private int type;
	// 方块的方向
	private int direction;
	// 方库的数据
	private int[][] shapes;

	
	//获得面板的大小
	ListBg listBg=ListBg.getListBg();
	int kuan=listBg.getKuan();
	int gao=listBg.getGao();
	int gezi=listBg.getGezi();
	
	// 初始化
	public Shape() {
		getShapeRandom();
	}

	//获得随机的
	private void getShapeRandom() {
		// 随机生成方块的参数
		// 方块的类型
		type = (int) (Math.random() * 7);
		// 方块的方向
		direction = (int) (Math.random() * 4);

		// 得到方块的数据
		shapes = getRandomShape();
	}

	// 改变方块的方向
	public void updateDirection() {
		direction = ++direction == 4 ? 0 : direction;
		// 获得现在的坐标
		int x = shapes[0][0];
		int y = shapes[0][1];
		shapes = getRandomShape();
		for (int i = 0; i < shapes.length; i++) {
			shapes[i][0] = shapes[i][0] + x;
			shapes[i][1] = shapes[i][1] + y;
		}
	}

	/* 随机得到一个俄罗斯方块的数组 */
	public int[][] getRandomShape() {
		// 生成
		switch (type) {

		// 方块
		case 0:
			// 方向
			switch (direction) {
			// 上
			case 0:
			default:
				shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } };
				break;

			}
			break;

		// 长条
		case 1:
			// 方向
			switch (direction) {
			// 上下
			case 0:
			case 2:
				shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 } };
				break;
			// 左右
			default:
				shapes = new int[][] { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 } };
				break;
			}
			break;

		// T型
		case 2:
			// 方向
			switch (direction) {
			// 上
			case 0:
				shapes = new int[][] { { 0, 1 }, { 1, 0 }, { 1, 1 }, { 1, 2 } };
				break;
			// 下
			case 1:
				shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 1 } };
				break;
			// 左
			case 2:
				shapes = new int[][] { { 1, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 } };
				break;
			// 右
			case 3:
				shapes = new int[][] { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 1, 1 } };
				break;
			}
			break;

		// 正L
		case 3:
			// 方向
			switch (direction) {
			// 上
			case 0:
				shapes = new int[][] { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 1, 2 } };
				break;
			// 下
			case 1:
				shapes = new int[][] { { 0, 1 }, { 1, 1 }, { 2, 1 }, { 2, 0 } };
				break;
			// 左
			case 2:
				shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 2 } };
				break;
			// 右
			case 3:
				shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 2, 0 } };
				break;
			}
			break;

		// 反L
		case 4:
			// 方向
			switch (direction) {
			// 上
			case 0:
				shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 } };
				break;
			// 下
			case 1:
				shapes = new int[][] { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 2, 1 } };
				break;
			// 左
			case 2:
				shapes = new int[][] { { 0, 2 }, { 1, 0 }, { 1, 1 }, { 1, 2 } };
				break;
			// 右
			case 3:
				shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 } };
				break;
			}
			break;

		// 正Z
		case 5:
			// 方向
			switch (direction) {
			// 上下
			case 0:
			case 2:
				shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 1, 2 } };
				break;
			// 左右
			default:
				shapes = new int[][] { { 0, 1 }, { 1, 0 }, { 1, 1 }, { 2, 0 } };
				break;
			}
			break;

		// 反Z
		case 6:
			// 方向
			switch (direction) {
			// 上下
			case 0:
			case 2:
				shapes = new int[][] { { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 } };
				break;

			// 左右
			default:
				shapes = new int[][] { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 2, 1 } };
				break;

			}
			break;
		}
		// 返回生成的数组
		return shapes;
	}

	// 让方块下移
	public void toXia() {
		//shapes = new int[][] { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 1 } };
		//获得方块最下边的那个小格子的x和y坐标
		int shapeX=shapes[0][0];
		int shapeY=shapes[0][1];
		for(int i=0;i<shapes.length;i++){
			if(shapeY<shapes[i][1]){
				shapeY=shapes[i][1];
				shapeX=shapes[i][0];
			}
		}
		System.out.println("x,y:"+shapeX+","+shapeY);
			
		
		//获得和方块最下边格子在一列的第一个实心背景格子
		int bgY=listBg.getBg()[0].length-1;
		
		System.out.println("bgY:"+bgY);
		
		for(int i=listBg.getBg()[0].length-1;i>=0;i--){
			if(listBg.getBg()[shapeX][i]==1){
				bgY=i;
			}
		}
		
		System.out.println("bgY2:"+bgY);
		
		
		if(shapeY<bgY-1){
			for (int i = 0; i < shapes.length; i++) {
				shapes[i][1] = shapes[i][1] + 1;
			}
		}
		else{
			listBg.updateBg(this);
			getShapeRandom();
		}
		
		
	}

	// 让方块左移
	public void toZuo() {
		for (int i = 0; i < shapes.length; i++) {
			shapes[i][0] = shapes[i][0] - 1;
		
		}
	}

	// 让方块右移
	public void toYou() {
		for (int i = 0; i < shapes.length; i++) {
			shapes[i][0] = shapes[i][0] + 1;
			
		}
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public int[][] getShapes() {
		return shapes;
	}

	public void setShapes(int[][] shapes) {
		this.shapes = shapes;
	}

}
